<?php

namespace app\api\controller\order;

use app\api\controller\Controllers;
use app\api\service\order\ShoppingCart;
use app\api\service\system\System;
use app\api\service\user\Address;
use app\api\service\order\Order as OrderService;
use app\exceptions\OrdinaryException;

class Order extends Controllers
{

    /**
     * 确认订单
     *
     * @return \think\Response
     */
    public function confirm(ShoppingCart $shoppingCart,System $system)
    {
        if ( false === $system->rest() ) {
            return failed('商家已休息');
        }
        $order = $shoppingCart->confirm();
        $pack_fee = array_sum(array_map(function($v){
            return $v['number']*$v['foods']['pack_fee'];
        }, $order->toArray()));
        $distribution_fee = $system->systemSet('distribution_fee');
        $price = $pack_fee+$distribution_fee+array_sum(array_map(function($v){
            return $v['number']*$v['foods']['price'];
        }, $order->toArray()));
        return success([
            'lists'=> $order,
            'distribution_fee' => $distribution_fee,
            'pack_fee' => $pack_fee,
            'price' => $price
        ]);
    }

    /**
     * 下单
     *
     * @return \think\Response
     */
    public function setOrder(ShoppingCart $shoppingCart, System $system, Address $address, OrderService $service)
    {
        if ( false === $system->rest() ) {
            return failed('商家已休息');
        }
        $post = $this->request->only(['price','aid','remarks'], 'post');
        $this->validate($post, 'app\api\validate\order\Order.setOrder');
        $lists = $shoppingCart->confirm();
        $distribution_fee = $system->systemSet('distribution_fee');
        $price = array_sum(array_map(function($v){
            if ( $v['number'] > $v['foods']['stock'] ) {
                throw new OrdinaryException('库存不足...');
            }
            return $v['number']*$v['foods']['price']+$v['number']*$v['foods']['pack_fee'];
        }, $lists->toArray()));
        if ( $price+$distribution_fee != $post['price'] ) {
            return failed('金额比对出错!,请稍后再试');
        }
        $addressData = $address->read($post['aid']);
        if ( ! $addressData ) {
            return failed('收货地址不存在');
        }
        return success($service->setOrder($post, $lists, $distribution_fee, $addressData));
    }

    /**
     * 订单列表
     *
     * @return \think\Response
     */
    public function lists(OrderService $service)
    {
        $get = $this->request->get();
        $page = $service->lists($get);
        foreach ( $page as $key => $data ) {
            $page[$key]['count'] = count($data['order_foods']);
            $page[$key]['cancel'] = strtotime($data['create_at'])+60*30>time();
        }
        return success($page);
    }

    /**
     * 订单详情
     * @param int $id
     * @return \think\Response
     */
    public function read(int $id, OrderService $service, System $system)
    {
        return success([
            'data' => $service->read($id),
            'mobile' => $system->systemSet('mobile')
        ]);
    }

    /**
     * 取消订单
     * @param int $id
     * @return \think\Response
     */
    public function cancel(int $id, OrderService $service)
    {
        return $service->cancel($id)?
            success():
            failed('取消失败');
    }

}
